
$(function(){
    get_user_info();
    bind_password_form();

    window.onresize = function () {
        changeFrameHeight();
    }

   // window.setInterval("changeFrameHeight()", 200);
});

function tglClick(obj) {
    console.log(obj.id);
    var iframe = document.getElementById("frameContext");
    switch(obj.id)
    {
        case "projTgl":
            iframe.src = "../htmls/main/my_project.html";
            break;
        case "dataTgl":
            iframe.src = "../htmls/main/my_data.html";
            break;
        case "appTgl":
            iframe.src = "../htmls/main/apps_center.html";
            break;
        case "toolTgl":
            iframe.src = "../htmls/main/tools_center.html";
            break;
        case "helpTgl":
            iframe.src = "../htmls/main/helps_center.html";
            break;
        default:
            break;
    }

    changeFrameHeight();
}

function changeFrameHeight(frame) {
    //frame.height= document.getElementById("frameContext").ownerDocument.body.scrollHeight;
   /*
    var ifm = document.getElementById("frameContext");
    ifm.height = document.documentElement.clientHeight-56; //56是头顶的高度，和网友估计不一样。
  */
    var iframe = document.getElementById("frameContext");
    try{
        var bHeight = iframe.contentWindow.document.body.scrollHeight;
        var dHeight = iframe.contentWindow.document.documentElement.scrollHeight;
        var height = Math.max(bHeight, dHeight);
        iframe.height = height;
         console.log("height  " + iframe.height);
    }catch (ex){}
}

function exist_login() {
    ClearUserParam();
}

function get_user_info()
{
    var user_id = getUserID(); //window.localStorage.getItem("user_id");
    console.log(user_id);
    if(user_id == null || user_id=="")
        return ;

    // 用户权限
    $.ajax({
        type: "POST",
        url: "http://47.104.102.188:7979/api/user/permissions/get",
        data: { login_id:user_id },
        dataType: "json",
        beforeSend: setRequestHeader,
        success: function(data){
            console.log(JSON.stringify(data));
            for(var i=0; i<data.data.length; ++i) {
                if(data.data[i].permission_code == "CREATE_INVITE_CODE") {
                    $("#manager_center_id").css("visibility", "visible")
                }
            }

        },
        error : function (err) {

        }
    });

    // 用户信息
    $.ajax({
        type: "POST",
        url: "http://47.104.102.188:7979/api/user/get_by_login_id",
        data: { login_id:user_id },
        dataType: "json",
        beforeSend: setRequestHeader,
        success: function(res){
            console.log(JSON.stringify(res));

            $("#user_id_name").html(res.data.user_name);
            $("#user_id_name2").html(res.data.user_name);
            $("#user_email").html(res.data.user_email);
        },
        error : function (err) {

        }
    });
}


function  manager_center() {
    $("#invite-dialog").css("display", "flex");
}

function user_center() {
    $(".user_center").css("display", "block");
}

function change_password() {
    $(".user_center").css("display", "none");
    $('#password-dialog').css("display", "flex");
}

function changeCancel() {
    $('#password-dialog').css("display", "none");
    document.getElementById("password_form").reset();
}

function bind_password_form()
{
    $('#password_form').bind('submit', function(){
        password_submit(this, function(res){
            console.log(JSON.stringify(res));
            if(res.success)
            {
                MessageBox("show", "操作成功", "密码修改成功!");

                $('#password-dialog').css("display", "none");
                document.getElementById("password_form").reset();

            } else if(res.code == "3-001") {
                MessageBox("show", "发生错误", res.message);
            } else if(res.code == "2-007") {
                console.log("令牌过期");
            }

        }, function (err) {
            alert(err);
        });
        return false;
    });

}

//将form转为AJAX提交
function password_submit(frm, fn, err)
{
    var jsonParam = getFormJson(frm);

    jsonParam.current_password = hex_md5(jsonParam.current_password);
    jsonParam.new_password = hex_md5(jsonParam.new_password);
    jsonParam.new_password2 = hex_md5(jsonParam.new_password2);
    console.log(jsonParam);

    if(jsonParam.new_password != jsonParam.new_password2)
    {
        MessageBox("show", "错误", '两次输入的密码不同！');
    }
    else
    {
        var user_id = getUserID();
        console.log(user_id);
        if(user_id == null || user_id=="")
            return ;

        jsonParam["login_id"] = user_id;

        $.ajax({
            url: frm.action,
            //xhrFields:{withCredentials:true},
            type: frm.method,
            data: jsonParam,
            beforeSend: setRequestHeader,
            success: fn,
            error : err
        });
    }
}

function gen_invite_code()
{
    var token = getToken();

    if(token == null || token =="") {
        console.log("请先登录");
        return;
    }

    $("#invidte-label").html("");

    $.ajax({
        url: "http://47.104.102.188:7979/api/admin/invite_code/create",
        //xhrFields:{withCredentials:true},
        type: "POST",
        data: null,
        beforeSend: setRequestHeader,
        success: function (res) {
            if(res.success) {
                $("#invidte-label").html(res.data);
            }
        },
        error : function (err) {
            
        }
    });
}




